package edu.ujf.sharepas.rcp;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.TableViewer;

import edu.ujf.sharepas.impl.User;
import edu.ujf.sharepas.rmi.Client;
import edu.ujf.sharepas.rmi.Serveur;

public class UsersView extends ViewPart implements Observer {

	public static final String ID = "edu.ujf.sharepas.rcp.usersView";
	private TableViewer tableViewer = null;
	private Serveur serveur ;  //  @jve:decl-index=0:
	private Client client ;  //  @jve:decl-index=0:
	private User user ;
	
	@Override
	public void createPartControl(Composite parent) {

        tableViewer = new TableViewer(parent, SWT.LEFT);
        tableViewer.setContentProvider(new UserListContentProvider());
        tableViewer.setLabelProvider(new UserListLabelProvider());
        tableViewer.setInput(getInitialInput());

	}

	private Object getInitialInput() {
		return user.getFriends();
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}
	
	@Override
	public void init(IViewSite site) throws PartInitException {
		super.init(site);
		client = Application.c ;
		try {
			user = client.getUser() ;
			user.addObserver(this);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void update(final Observable arg0, Object arg1) {
		
		Display.getDefault().asyncExec(new Runnable(){
			@Override
			public void run() {
				tableViewer.setInput(((User)arg0).getFriends());
			}
		});
		
	}

	private void setServeur(Serveur s) {
		this.serveur = s;
	}

}
